package com.youxue.controller;

import com.alibaba.fastjson.JSONObject;
import com.cangshi.permission.entity.UserSubject;
import com.youxue.entity.*;
import com.youxue.permission.saver.SubjectHolder;
import com.youxue.service.UserClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by 15c on 2018/1/28.
 */
@Controller
@RequestMapping("/userclass")
public class UserClassController {
    @Autowired
    UserClassService userClassService;

    /**
     *展示一个登陆用户所学习过的课程列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/show", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String showAUserClass() {
        // 获取当前访问对象
        UserSubject subject = (UserSubject) SubjectHolder.getSubject();

        //获取当前的登录信息
        UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
        //获取当前的用户登录id
        int userId = userEntity.getUserId();
        List<UserClass> list=userClassService.showAUserClass(userId);

        CorrectJsonObject correctJsonObject=new CorrectJsonObject("查询成功");
        correctJsonObject.setData(list);
        return JSONObject.toJSONString(correctJsonObject);
    }

    /**
     * 删除学习记录
     * @param classId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String reviseAUserClass(@RequestParam(required = false) Integer  classId){
        // 获取当前访问对象
        UserSubject subject = (UserSubject) SubjectHolder.getSubject();

        //获取当前的登录信息
        UserEntity userEntity = (UserEntity) subject.getAuthorizationEntity();
        //获取当前的用户登录id
        int userId = userEntity.getUserId();
        if (null==classId){
            ErrorJsonObject errorJsonObject=new ErrorJsonObject(101,"classId参数为空");
            return JSONObject.toJSONString(errorJsonObject);
        }
        UserClassKey userClassKey=new UserClassKey();
        userClassKey.setUserId(userId);
        userClassKey.setClassId(classId);
        int result=userClassService.reviseAUserClass(userClassKey);
        if(1!=result){
            ErrorJsonObject errorJsonObject=new ErrorJsonObject(102,"删除失败");
            return JSONObject.toJSONString(errorJsonObject);
        }
        CorrectJsonObject correctJsonObject=new CorrectJsonObject("删除成功");
        return JSONObject.toJSONString(correctJsonObject);
    }
}
